package cn.whit.blog.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.whit.blog.dao.StatisticalDao;
import cn.whit.blog.domain.Statistical;
import cn.whit.blog.domain.StatisticalType;
import cn.whit.blog.util.JDBCUtils;

public class StatisticalDaoImpl implements StatisticalDao{

	@Override
	public List<Statistical> findAllByUserIdAndTblogIdAndType(String userid, String blogid, StatisticalType statisticalType) throws SQLException {
		String sql="select * from statistical where userid = ? and contactid = ? and contacttype = ? ";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanListHandler<Statistical>(Statistical.class),userid,blogid,statisticalType.toString());
	}
	
	@Override
	public List<Statistical> findAllByTblogIdAndType(String blogid, StatisticalType statisticalType) throws SQLException {
		String sql="select * from statistical where contactid = ? and contacttype = ? ";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanListHandler<Statistical>(Statistical.class),blogid,statisticalType.toString());
	}

	@Override
	public void add(String userid,String username,  String id, StatisticalType statisticalType) throws SQLException {
		String sql="INSERT INTO statistical(contactid,contacttype,status,userid,operTime,username) "
				+ " VALUES(?,?,?,?,?,?)";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		Object[] params={id,statisticalType.toString(),0,userid,new Date(),username};
		qr.update(sql,params);
	}

	@Override
	public void deleteByVoteId(String s) throws SQLException {
		String sql="delete from statistical where contactid=? ";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		Object[] params={s};
		qr.update(sql,params);
	}

}
